from random import randint

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()

# 获取随机的打分，默认给4-5分 满意和非常满意
def get_number(low=4, high=5):
    number = randint(low, high)
    return number


# 选择题
def click_path():
    path_list = []
    for ii in range(92, 102):
        # 这题得选是
        if ii == 98:
            path = "//*[@id='" + str(ii) + "']/div[1]/label/span[1]/input"
        else:
            path = "//*[@id='" + str(ii) + "']/div[" + str(get_number()) + "]/label/span[1]/input"
        path_list.append(path)
        print(path)
    return path_list

# 填空题
def write_path():
    write_list = []
    write_list.append("//*[@id='112']")
    return write_list


# 捕获异常
def NodeExists(xpath):
    try:
        driver.find_element_by_xpath(xpath)
        return True
    except:
        return False


if __name__ == '__main__':
    url = 'http://pingjiao.bit.edu.cn/'
    driver.get(url)
    time.sleep(6)
    # 模拟登录 修改你的学号的密码
    driver.find_element_by_xpath("//*[@id='username']").send_keys('3220201XXX')
    driver.find_element_by_xpath("//*[@id='password']").send_keys('XXX')
    time.sleep(1)
    driver.find_element_by_xpath("//*[@id='login_submit']").click()


    for i in range(20):
        # 评教主界面
        time.sleep(10)
        url2 = 'http://pingjiao.bit.edu.cn/index.html?v=2.23.2/#/my-task/details/UnFinished/1/Final/10?semester=2020-2021-1'
        driver.get(url2)
        time.sleep(6)

        # 单独评教界面
        # url3 = 'http://pingjiao.bit.edu.cn/index.html?v=2.23.2/#/my-task/answer/zkd/1/1/Final/10/0700004?semester=2020-2021-1'
        # driver.get(url3)
        driver.find_element_by_xpath('//*[@id="main-layout-inner-content-main-inner-3jeur"]/div[1]/div/div[3]/div/div/div[3]/div[1]/div[2]/div/div/div/div/div/div/div/table/tbody/tr[1]/td[7]/span').click()
        time.sleep(6)
        xpath_click = click_path()
        xpath_write = write_path()

        # 填写问卷
        for cc in xpath_click:
            driver.find_element_by_xpath(cc).click()
        for ww in xpath_write:
            driver.find_element_by_xpath(ww).send_keys('老师很好，没有其他建议')

        # 处理一门课有两个导师的情况
        if NodeExists('//*[@id="main-layout-inner-content-main-inner-3jeur"]/div[1]/div/div[3]/div/div/div[3]/div[1]/form/div/div[15]/button'):
            driver.find_element_by_xpath('//*[@id="main-layout-inner-content-main-inner-3jeur"]/div[1]/div/div[3]/div/div/div[3]/div[1]/form/div/div[15]/button').click()
        if NodeExists('//*[@id="main-layout-inner-content-main-inner-3jeur"]/div[1]/div/div[3]/div/div/div[3]/div[2]/form/div/div[15]/button'):
            driver.find_element_by_xpath('//*[@id="main-layout-inner-content-main-inner-3jeur"]/div[1]/div/div[3]/div/div/div[3]/div[2]/form/div/div[15]/button').click()
        time.sleep(5)
